﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CarmeraBind : MonoBehaviour
{
    public Transform bindControllor;
    public float Ydeviation;
    public Transform rotatorBall;
    //public Transform rotatorForward;
    public Transform bindCarmera;

    public bool isGetCamera=true;

    private Transform cameraPos;
    private Vector3 totalRight = new Vector3();

    // Start is called before the first frame update
    void Start()
    {
        //if(bindCarmera==null)
        //    bindCarmera = transform.Find("ControllerCamera");

        //对旋转物体按次序进行绑定
        //if (bindCarmera != null)
        //{
        //    if (rotatorBall != null)
        //    {
        //        bindCarmera.parent = rotatorBall;
        //        //if (rotatorForward != null)
        //        //    rotatorUp.parent = rotatorForward;
        //    }
        //}
        //rotatorBall.position = new Vector3(bindControllor.position.x, bindControllor.position.y + Ydeviation, bindControllor.position.z);
        //rotatorBall.rotation = new Quaternion(bindControllor.rotation.x, bindControllor.rotation.y, bindControllor.rotation.z, bindControllor.rotation.w);
        cameraPos = rotatorBall.Find("CameraPos");
    }

    // Update is called once per frame
    void Update()
    {
        //使rotatorForward摄像球跟着操纵对象一起移动
        rotatorBall.position = new Vector3(bindControllor.position.x, bindControllor.position.y + Ydeviation, bindControllor.position.z);
        if (isGetCamera)
        {
            bindCarmera.position = cameraPos.position;
            bindCarmera.rotation = cameraPos.rotation;
        }

        float mouseX = Input.GetAxis("Mouse X")*1000f;
        float mouseY = Input.GetAxis("Mouse Y")*1000f;
        //transform.rotation
        //rotatorBall.Rotate(Vector3.forward * (mouseY) * Time.deltaTime);
        //print(rotatorBall.rotation.x);
        //rotatorUp.rotation = Quaternion.Euler(rotatorUp.rotation.x, rotatorUp.rotation.y, rotatorUp.rotation.z+mouseY*Time.deltaTime);
        rotatorBall.Rotate(Vector3.up * mouseX * Time.deltaTime);
        //if(cameraPos.localRotation.x>-75f)

        //限制纵向旋转角度
        if (-mouseY > 0)
        {
            if (totalRight.x < 35)  //向下旋转最大35度
            {
                cameraPos.Rotate(Vector3.right * (-mouseY) * Time.deltaTime);
                totalRight += Vector3.right * (-mouseY) * Time.deltaTime;
            }
        }
        else
        {
            if (totalRight.x > -60) //向上旋转最大60度
            {
                cameraPos.Rotate(Vector3.right * (-mouseY) * Time.deltaTime);
                totalRight += Vector3.right * (-mouseY) * Time.deltaTime;
            }
        }

        //print(totalRight);
    }
}
